import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import com.mysql.jdbc.ResultSet;

/**
 * This class is Define Assignment UI
 */
public class  DefineAssignmentUI  extends JPanel{
	/**
	 * chosen option for course
	 */
	private JComboBox courseBox = null;
	/**
	 * choose date
	 */
	private JComboBox dayBox = null;
	private JComboBox monthBox = null;
	private JComboBox yearBox = null;
	/**
	 * define course button
	 */
	private JButton btnDefine = null;
	/**
	 * browse  button
	 */
	private JButton btnBrowse = null;
	/**
	 * back button
	 */
	public JButton btnBack = null;
	/**
	 * title for the current window  
	 */
	private JLabel lbltitle = null;
	/**
	 * help button 
	 */
	public JButton btnHelp = null;
	/**
	 * list of class and course 
	 */
	private ArrayList<String[]> l = new ArrayList<String[]>();
	/**
	 * choose course
	 */
	private int courseFlg;
	private int day=1;
	private int month=1;
	private int year=2012;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	/**
	 *  the loaded file
	 */
	private MyFile file;
	/**
	 *  logout button
	 */
	public JButton btnLogout = null;
	
	public DefineAssignmentUI (){
		super();
		initializeEmpty();
	}

	private void initializeEmpty() {
		curPanel = this;
		lbltitle = new JLabel("Define Assignment");
		lbltitle.setBounds(180, 28, 110, 14);
		this.add(lbltitle);
		
		JLabel lblNoCourses= new JLabel("There aren't any courses");
		lblNoCourses.setBounds(135,88, 190, 14);
		this.add(lblNoCourses);
		
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnLogout = new JButton("Logout");     
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose course type and click on 'Browse' button or ","on 'Define New Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		this.setVisible(true);
		
		
		this.setSize(400, 321);
		this.setLayout(null);
		
	}

	public DefineAssignmentUI(Object list) {
		super();
		l = (ArrayList) list;
		initialize();
	}

	private void initialize() {
		curPanel = this;
		
		this.add(getCourseBox(), null);
		
		this.add(getDayBox(), null);
		JLabel lblDay = new JLabel();
		lblDay.setBounds(new Rectangle(80, 125, 95, 25)); 
		this.add(lblDay);
		this.add(getMounthBox(), null);
		this.add(getYearBox(), null);
		
		lbltitle = new JLabel("Define Assignment");
		lbltitle.setBounds(180, 28, 110, 14);
		this.add(lbltitle);
		btnBrowse = new JButton();
		btnBrowse.setBounds(new Rectangle(240, 85,  86, 25));
		btnBrowse.setText("Browse");
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Browse b = new Browse();
				file = b.getFile();
				System.out.println("file is" +file.getName()); 
			}	
		});
		this.add(btnBrowse);
		
		
		btnDefine = new JButton();
		btnDefine.setBounds(new Rectangle(130, 180, 195, 25));
		btnDefine.setText("Define New Assignment");
		btnDefine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (file != null)
				{
					String date = day +"."+month+"."+year;
					Assignment assginment = new Assignment(l.get(courseFlg)[1],1,file.getName(),Global.logIn.getUserlogin(),date,file.getMybytearray(),file.getSize());
					SQL s = new SQL (assginment,19);
					s.accept();
					if (s.getMsg().equals(true))
					{
						MsgWin msgWin = new MsgWin("Your new Assignment was added"); 	
					}
					
				}
			
			}
		});
		this.add(btnDefine);
	
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnLogout = new JButton("Logout");     //Logout button
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose course type and click on 'Browse' button or ","on 'Define New Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		this.setVisible(true);
		
		
		this.setSize(400, 321);
		this.setLayout(null);
		
	}
	
	private JComboBox getCourseBox() {
		if (courseBox == null) {
			courseBox = new JComboBox();
			courseBox.setBounds(new Rectangle(126, 85, 95, 25));  
			int size = l.size();
			String cID;
				for (int i=0; i<size ; i++)
				{
					cID = l.get(i)[1];
					SQL s = new SQL (cID,18);
					s.accept();
					Object msg = s.getMsg();
					System.out.println("this course : " + i + cID); 
					courseBox.addItem((String)msg);
				}
				
			courseBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					courseFlg = courseBox.getSelectedIndex();
					System.out.println("this is : " + courseFlg); 
				}
			});
		}
		return courseBox;
	}
	
	private JComboBox getDayBox() {
		if (dayBox == null) {
			dayBox = new JComboBox();
			dayBox.setBounds(new Rectangle(126, 125, 45, 25));  
				for (Integer i=1; i<32 ; i++)
				{
					dayBox.addItem(i.toString());
				}
				
				dayBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					day = dayBox.getSelectedIndex();
					System.out.println("day is : " + day); 
				}
			});
		}
		return dayBox;
	}
	
	private JComboBox getMounthBox() {
		if (monthBox == null) {
			monthBox = new JComboBox();
			monthBox.setBounds(new Rectangle(186, 125, 45, 25));  
				for (Integer i=1; i<13 ; i++)
				{
					monthBox.addItem(i.toString());
				}
				
				monthBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					month = monthBox.getSelectedIndex();
					System.out.println("month is : " + month); 
				}
			});
		}
		return monthBox;
	}
	
	private JComboBox getYearBox() {
		if (yearBox == null) {
			yearBox = new JComboBox();
			yearBox.setBounds(new Rectangle(246, 125, 45, 25));  
				for (Integer i=2012; i<2030 ; i++)
				{
					yearBox.addItem(i.toString());
				}
				
				yearBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					year = yearBox.getSelectedIndex();
					System.out.println("year is : " + year); 
				}
			});
		}
		return yearBox;
	}
		
}
	